//
//  LCLanguageModule.m
//  Pods
//
//  Created by 裘依琳 on 2021/3/25.
//

#import "LCLanguageModule.h"
#import <LCModuleManager/LCModuleManager.h>

@interface LCLanguageModule()<LCModuleProtocol>


@end


@implementation LCLanguageModule

- (void)moduleOnInit
{
    
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    /// 设置语言
    [self getlanguage];
}


- (void)getlanguage{
    NSArray  *languages = [NSLocale preferredLanguages];
    //判断当前系统语言
    NSString *language = [languages objectAtIndex:0];
    /// 判断上一次系统语言
    NSString *lastSysLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:@"systemLanguage"];

    BOOL sysIsChange = NO;
    if (![language containsString:lastSysLanguage]) {
        sysIsChange = YES;
        /// 设置系统语言
        [[NSUserDefaults standardUserDefaults] setObject:language forKey:@"systemLanguage"];
    }
    
    NSInteger type = [[NSUserDefaults standardUserDefaults] integerForKey:@"appLanguageType"];
    if (type == 1 && !sysIsChange) {
        language = [[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"];
    }
    
    if ([language hasPrefix:@"zh"]) {
        //我这里的需求是，如果不是中文，则APP语言都显示成英文，
        //先把它存在NSUserDefaults中
        [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"appLanguage"];
    } else if ([language hasPrefix:@"fr"]){
        [[NSUserDefaults standardUserDefaults] setObject:@"fr" forKey:@"appLanguage"];
    } else if ([language hasPrefix:@"ja"]){
        [[NSUserDefaults standardUserDefaults] setObject:@"ja" forKey:@"appLanguage"];
    } else if ([language hasPrefix:@"ko"]){
        [[NSUserDefaults standardUserDefaults] setObject:@"ko" forKey:@"appLanguage"];
    } else if ([language hasPrefix:@"tr"]){
        [[NSUserDefaults standardUserDefaults] setObject:@"tr" forKey:@"appLanguage"];
    } else{
        //en.lproj 是英文资源包的文件名字
        [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"appLanguage"];
    }
}


@end
